package com.scau.os.fileSystem.directory;

import java.util.ArrayList;
import java.util.List;

public class Path {

    public static final Path ROOT_PATH = new Path("");
    private String absolutePath;

    public Path(String absolutePath) {
        this.absolutePath = absolutePath.trim();
    }

    public String getAbsolutePath() {
        return absolutePath.trim();
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public List<String> getPathNames() {
        List<String> pathNames = new ArrayList<>();
        String[] names;
        if (absolutePath.contains(".")) {
            names = absolutePath.substring(0, absolutePath.lastIndexOf(".")).split("/");
        } else {
            names = absolutePath.split("/");
        }
        for (String name : names) {
            if (name.isBlank() == false) {
                pathNames.add(name);
            }
        }
        return pathNames;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((absolutePath == null) ? 0 : absolutePath.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Path other = (Path) obj;
        if (absolutePath == null) {
            if (other.absolutePath != null)
                return false;
        } else if (!absolutePath.trim().equals(other.absolutePath.trim()))
            return false;
        return true;
    }

}
